package com.dxp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxp.common.CustomException;
import com.dxp.mapper.CategoryMapper;
import com.dxp.pojo.Category;
import com.dxp.pojo.Dish;
import com.dxp.pojo.Setmeal;
import com.dxp.service.CategoryService;
import com.dxp.service.DishService;
import com.dxp.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setMealService;
    @Override
    public void remove(Long id){
        //如果该分类关联了菜品,此时不允许删除，抛出一个异常，提示用户
        QueryWrapper<Dish> dishQueryWrapper = new QueryWrapper<>();
        dishQueryWrapper.eq("category_id",id);
        int count1 = dishService.count(dishQueryWrapper);
        if(count1>0)
        {
            throw new CustomException("该分类关联有菜品,暂时无法删除");
        }
        //如果该分类关联了套餐,此时不允许删除，抛出一个异常，提示用户
        QueryWrapper<Setmeal> setmealQueryWrapper = new QueryWrapper<>();
        setmealQueryWrapper.eq("category_id",id);
        int count2 = setMealService.count(setmealQueryWrapper);
        if(count2>0)
        {
            throw new CustomException("该分类关联有套餐,暂时无法删除");
        }
        super.removeById(id);
    }
}
